package com.gxks.lhs.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Order;
import com.gxks.lhs.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-19
 */
@Api(tags = "订单管理模块")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @ApiOperation("根据id取消订单")
    @PutMapping("/{id}")
    public R cancelOrderById(@PathVariable(value = "id") String id) {
        boolean b = orderService.cancellationOfOrderById(id);
        if (b) {
            return R.success("取消成功");
        }
        return R.error("取消失败");
    }

    @ApiOperation("订单退款")
    @PutMapping("/refund/{id}")
    public R refundOrderById(@PathVariable(value = "id") Integer id) {
        boolean b = orderService.refundOrderById(id);
        if (b) {
            return R.success("退欯后成功");
        }
        return R.error("退欯后失败");
    }

    @ApiOperation("根据条件查询订单")
    @GetMapping("/search")
    public R findOrders(@RequestParam(value = "id", required = false) String id,
                        @RequestParam(value = "status", required = false) Integer status,
                        @RequestParam(value = "beginTime", required = false) Instant beginTime,
                        @RequestParam(value = "endTime", required = false) Instant endTime,
                        @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                        @RequestParam(value = "size", required = false, defaultValue = "10") Integer size
    ) {
        Page<Order> orders = orderService.findOrders(id, status, beginTime, endTime, current, size);
        return R.success(orders, "查询成功");
    }

    @ApiOperation("查询所有的订单")
    @GetMapping("/list")
    public R findAllOrders(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Order> list = orderService.findAllOrders(current, size);
        if (list.getTotal() == 0) {
            return R.error("查询失败");
        }
        return R.success(list, "查询成功");
    }

    @ApiOperation("根据id查询订单详细信息")
    @GetMapping("/{id}")
    public R findOrderById(@PathVariable(value = "id") String id) {
        Order order = orderService.findOrderByIdOfAdmin(id);
        if (order == null) {
            return R.error("查询失败");
        }
        return R.success(order, "查询成功");
    }

}

